package www.wen.com.common.lib.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import www.wen.com.common.R

/**
 * @author WEN
 * @Description: 自定义的手机流程绑定控件
 * @date 2021/8/28 18:02
 */
class StepView : ConstraintLayout {

    private  var mViewStep1: View
    private  var mViewStep2: View
    private  var mViewStep3: View

    private  var mTvStep1: TextView
    private  var mTvStep2: TextView
    private  var mTvStep3: TextView

    private  var mLineStep1: View
    private  var mLineStep2: View

    private var mStep: Int = 0

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        LayoutInflater.from(context).inflate(R.layout.layout_step_view, this)
        mViewStep1 = findViewById(R.id.viewStep1)
        mViewStep2 = findViewById(R.id.viewStep2)
        mViewStep3 = findViewById(R.id.viewStep3)
        mTvStep1 = findViewById(R.id.tvStep1)
        mTvStep2 = findViewById(R.id.tvStep2)
        mTvStep3 = findViewById(R.id.tvStep3)
        mLineStep1 = findViewById(R.id.lineStep1)
        mLineStep2 = findViewById(R.id.lineStep2)

        mViewStep1.isSelected = true
        mTvStep1.isSelected = true
        mLineStep1.isSelected = true
    }

    /**
     * 进入下一步流程
     */
    fun next() {
        if (mStep < 3)
            mStep++
        when(mStep){

            0 -> {
                mViewStep1.isSelected = true
                mTvStep1.isSelected = true
                mLineStep1.setBackgroundResource(R.drawable.shape_step_true)
            }

            1 -> {
                mLineStep1.setBackgroundResource(R.drawable.shape_step_finish)
                mLineStep2.setBackgroundResource(R.drawable.shape_step_true)
                mViewStep2.isSelected = true
                mTvStep2.isSelected = true
            }

            2 -> {
                mLineStep1.setBackgroundResource(R.drawable.shape_step_finish)
                mLineStep2.setBackgroundResource(R.drawable.shape_step_finish)
                mViewStep3.isSelected = true
                mTvStep3.isSelected = true
            }
        }
    }

    /**
     * 返回上一步流程
     */
    fun previous() {
        if (mStep > 0)
            mStep--
        when(mStep){

            0 -> {
                mViewStep1.isSelected = false
                mTvStep1.isSelected = false
                mLineStep1.isSelected = false
            }

            1 -> {
                mViewStep2.isSelected = false
                mTvStep2.isSelected = false
                mLineStep2.isSelected = false
            }

            2 -> {
                mViewStep3.isSelected = false
                mTvStep3.isSelected = false
            }
        }
    }

    fun getStep() = mStep

}